/*  TwitterBridge.java Version 102 - 20th May 2008
 *  TwitterBridge.java contains functions for communicating with Twitter
 *  See Versions for program version number
 *  Copyright (C) 2008  Andy Broomfield andy@newmediathinking.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package net.blogbotplatform.blogbot;

import java.io.*;
import java.net.*;

public class TwitterBrigde {
	//Twitter account variables
	//sorry guys, you will need to include your own here!
	//this should eventually be user configurable, either in a config file, or the GUI
	/*private final static String myUsername = "";
	//private final static String myScreename = "";
	private final static String myPassword = ""; */
	
	long twitterTimer;
	int TWITTER_INTERVAL = 1000;
	
	private static String userPassword;
	private static String encoding;
		
	TwitterBrigde() {
		twitterTimer = System.currentTimeMillis() - TWITTER_INTERVAL;
		setTwitterAccount();
	}
	
	static void setTwitterAccount() {
		userPassword = BlogBotShop.twitterUsername + ":" + BlogBotShop.twitterPassword;
		encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
	}

	boolean checkAlive() {
		  System.out.println("Checking your Authentication");

		  String twitterOutput = "";
		  
		  try {
		    URL url = new URL("http://www.twitter.com/account/verify_credentials");
		    URLConnection myConnection = url.openConnection();
		    
		    myConnection.setRequestProperty("Authorization", "Basic " + encoding);
		    myConnection.setDoInput(true);
		    myConnection.setDoOutput(true);

		    BufferedReader in = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));
		    String inputLine;

		    System.out.println("connection: " + myConnection.toString());

		    while (true) {
		    	System.out.println("reading line..");
		    	inputLine = in.readLine();

		    	if(inputLine == null) {
		    		System.out.println("nothing there anymore");
		    		break;
		    	}

		    	System.out.println("hello" + inputLine);
		    	twitterOutput += inputLine;
		    }

		    in.close();
		  } 
		  catch (MalformedURLException me) {
			  System.out.println("MalformedURLException: " + me);
		  } 
		  catch (IOException ioe) {
			  System.out.println("IOException: " + ioe);
		  }

		  if (twitterOutput.equals("Authorized")) { 
			  System.out.println("Twitter Authutication had been Verifyed");
			  return true;
		  } else {
			  System.out.println("Access to Twitter API has been Denied");
			  return false;
		  }
	}

	void update(String whichText) {
		System.out.println("Making a Twitter update");
		
		if (System.currentTimeMillis() < twitterTimer + TWITTER_INTERVAL) {
			System.out.println("Too Quick! Post Slower!");
			return;
		}

		String updateText = "status=" + whichText;
		String twitterOutput = "";

		try {
			URL url = new URL("http://twitter.com/statuses/update.xml");
			URLConnection myConnection = url.openConnection();
			myConnection.setRequestProperty ("Authorization", "Basic " + encoding);
			// myConnection.setRequestMethod("POST");
			myConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			myConnection.setRequestProperty("Content-Length", "" + Integer.toString(updateText.getBytes().length));
			myConnection.setDoInput(true);
			myConnection.setDoOutput(true);

			DataOutputStream dos;
			//String outputLine;

			dos = new DataOutputStream(myConnection.getOutputStream());
			dos.writeBytes(updateText);
			dos.flush();
			dos.close();    

			DataInputStream dis;
			String inputLine;

			dis = new DataInputStream(myConnection.getInputStream());
			while ((inputLine = dis.readLine()) != null) {
				System.out.println(inputLine);
				twitterOutput += inputLine;
			}
			dis.close();
		} 
		catch (MalformedURLException me) {
			System.out.println("MalformedURLException: " + me);
		} 
		catch (IOException ioe) {
			System.out.println("IOException: " + ioe);
		}
		
		//some code needs to be here, to check we got back the xml we wanted
		/*if(twitterOutput.equals("Authorized")) { 
			System.out.println("Your Authutication had been Verifyed");
		} else {
			System.out.println("Access to Twitter API has been Denied");
		}*/
	}
}
